#!/bin/sh

echo "IntermediaCA1生成证书";
echo "1.生成example.com的msp";
echo "1）登记example.com";
cd ca-server
fabric-ca-client enroll -M ./crypto-config/ordererOrganizations/example.com/msp -u http://admin1:adminpw1@localhost:7055 --home ./fabric-ca-client
echo "2）添加联盟成员"
fabric-ca-client affiliation list -M ./crypto-config/ordererOrganizations/example.com/msp -u http://admin1:adminpw1@localhost:7055 --home ./fabric-ca-client
fabric-ca-client affiliation remove --force org1 -M ./crypto-config/ordererOrganizations/example.com/msp -u http://admin1:adminpw1@localhost:7055 --home ./fabric-ca-client
fabric-ca-client affiliation remove --force org2 -M ./crypto-config/ordererOrganizations/example.com/msp -u http://admin1:adminpw1@localhost:7055 --home ./fabric-ca-client
fabric-ca-client affiliation add com -M ./crypto-config/ordererOrganizations/example.com/msp -u http://admin1:adminpw1@localhost:7055 --home ./fabric-ca-client
fabric-ca-client affiliation add com.example -M ./crypto-config/ordererOrganizations/example.com/msp -u http://admin1:adminpw1@localhost:7055 --home ./fabric-ca-client

echo "2.生成Admin@example.com的msp"
echo "1）注册Admin@example.com"
fabric-ca-client register --id.name Admin@example.com --id.type client --id.affiliation "com.example" --id.attrs '"hf.Registrar.Roles=client,orderer,peer,user","hf.Registrar.DelegateRoles=client,orderer,peer,user",hf.Registrar.Attributes=*,hf.GenCRL=true,hf.Revoker=true,hf.AffiliationMgr=true,hf.IntermediateCA=true,role=admin:ecert' --id.secret=123456 --csr.cn=example.com --csr.hosts=['example.com'] -M ./crypto-config/ordererOrganizations/example.com/msp -u http://admin1:adminpw1@localhost:7055 --home ./fabric-ca-client
echo "2）登记Admin@example.com"
fabric-ca-client enroll -u http://Admin@example.com:123456@localhost:7055 --csr.cn=example.com --csr.hosts=['example.com'] -M ./crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp --home ./fabric-ca-client
echo "3）生成msp"
path=./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/admincerts
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/cert.pem ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/admincerts/Admin@example.com-cert.pem
path=./fabric-ca-client/crypto-config/ordererOrganizations/example.com/msp/admincerts/
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/cert.pem ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/msp/admincerts/Admin@example.com-cert.pem

echo "3.生成orderer0.example.com的msp"
echo "1）注册orderer0.example.com"
fabric-ca-client register --id.name orderer0.example.com --id.type orderer --id.affiliation "com.example" --id.attrs '"role=orderer",ecert=true' --id.secret=123456 --csr.cn=orderer0.example.com --csr.hosts=['orderer0.example.com'] -M ./crypto-config/ordererOrganizations/example.com/msp -u http://admin1:adminpw1@localhost:7055 --home ./fabric-ca-client

echo "2）登记orderer0.example.com"
fabric-ca-client enroll -u http://orderer0.example.com:123456@localhost:7055 --csr.cn=orderer0.example.com --csr.hosts=['orderer0.example.com'] -M ./crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp --home ./fabric-ca-client
echo "3）生成msp"
path=./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/admincerts
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/cert.pem ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/admincerts/Admin@example.com-cert.pem

echo "IntermediaCAtls1生成证书"
echo "1.生成example.com的tls"
echo "1）登记example.com"
cd /opt/gopath/src/github.com/hyperledger/fabric-ca/bin/ca-server
fabric-ca-client enroll -M ./crypto-config/ordererOrganizations/example.com/tlsca -u http://admin1:adminpw1@localhost:8055 --home ./fabric-ca-client
echo "2）添加联盟成员"
fabric-ca-client affiliation list -M ./crypto-config/ordererOrganizations/example.com/tlsca -u http://admin1:adminpw1@localhost:8055 --home ./fabric-ca-client
fabric-ca-client affiliation remove --force org1 -M ./crypto-config/ordererOrganizations/example.com/tlsca -u http://admin1:adminpw1@localhost:8055 --home ./fabric-ca-client
fabric-ca-client affiliation remove --force org2 -M ./crypto-config/ordererOrganizations/example.com/tlsca -u http://admin1:adminpw1@localhost:8055 --home ./fabric-ca-client
fabric-ca-client affiliation add com -M ./crypto-config/ordererOrganizations/example.com/tlsca -u http://admin1:adminpw1@localhost:8055 --home ./fabric-ca-client
fabric-ca-client affiliation add com.example -M ./crypto-config/ordererOrganizations/example.com/tlsca -u http://admin1:adminpw1@localhost:8055 --home ./fabric-ca-client

echo "2.生成Admin@example.com的tls"
echo "1）注册Admin@example.com"
fabric-ca-client register --id.name Admin@example.com --id.type client --id.affiliation "com.example" --id.attrs '"hf.Registrar.Roles=client,orderer,peer,user","hf.Registrar.DelegateRoles=client,orderer,peer,user",hf.Registrar.Attributes=*,hf.GenCRL=true,hf.Revoker=true,hf.AffiliationMgr=true,hf.IntermediateCA=true,role=admin:ecert' --id.secret=123456 --csr.cn=example.com --csr.hosts=['example.com'] -M ./crypto-config/ordererOrganizations/example.com/tlsca -u http://admin1:adminpw1@localhost:8055 --home ./fabric-ca-client
echo "2）登记Admin@example.com"
fabric-ca-client enroll -d --enrollment.profile tls -u http://Admin@example.com:123456@localhost:8055  --csr.cn=example.com --csr.hosts=['example.com'] -M ./crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tlsca --home ./fabric-ca-client
echo "3）生成tls"
path=./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tls
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./intermediacatls1/ca-chain.pem ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tls/ca.crt
cp ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tlsca/signcerts/cert.pem ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tls/client.crt
path=./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tlsca/keystore
keystore=`ls -l $path | tail -n 1 | awk '{print $9}'`
cp ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tlsca/keystore/$keystore ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tls/client.key
cp -r ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/tlsca/tlscacerts ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/msp



echo "3.生成orderer0.example.com的tls"
echo "1）注册orderer0.example.com"
fabric-ca-client register --id.name orderer0.example.com --id.type orderer --id.affiliation "com.example" --id.attrs '"role=orderer",ecert=true' --id.secret=123456 --csr.cn=orderer0.example.com --csr.hosts=['orderer0.example.com'] -M ./crypto-config/ordererOrganizations/example.com/tlsca -u http://admin1:adminpw1@localhost:8055 --home ./fabric-ca-client
echo "2）登记orderer0.example.com"
fabric-ca-client enroll -d --enrollment.profile tls -u http://orderer0.example.com:123456@localhost:8055 --csr.cn=orderer0.example.com --csr.hosts=['orderer0.example.com'] -M ./crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tlsca --home ./fabric-ca-client
echo "3）生成tls"
path=./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls
if [ ! -d $path ];then
      mkdir -p $path
fi
cp ./intermediacatls1/ca-chain.pem ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/ca.crt
cp ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tlsca/signcerts/cert.pem ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.crt
path=./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tlsca/keystore
keystore=`ls -l $path | tail -n 1 | awk '{print $9}'`
cp ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tlsca/keystore/$keystore ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/server.key
cp -r ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tlsca/tlscacerts ./fabric-ca-client/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/msp


#把tlsca换了
#msp/Admin@example.com-cert.pem
#中间证书删除